/**
 * Created by songaw on 2017/11/2.
 */





$(".docDownloadingBtn").click(function(){

 //   getContent();
 //  $("#docDownDiv").html();
   //重新绘制img
    $("#docDownDiv").wordExport();
});


/**
 * 导入Excel数据
 */
$(".fileUploadBtn").click(function(){
    var type=$(this).attr("data");
    var dataType=$(this).attr("dataSuffix");
    upload("/v1/convert/uploadFile",type,dataType);
});
function upload(url,type,dataType){
    var $fileInput = $("#fileUpload");


    // 异步提交表单
   var oFormData = new FormData();
    oFormData.append("uploadfile", $fileInput[0].files[0]);

    oFormData.append("type", type);


    var oAjaxOption = {
        type: "post",
        url: url,
        contentType: false,
        dataType: "json",
        data: oFormData,
        processData: false,
        success: function(data) {
            // $("#docDownDiv").html(data);
            var dataList=data.data;

            if(dataList!=null){
                for(var i=0;i<dataList.length;i++){
                    if(dataList[i].baseFile){
                        var baseFile=dataList[i].baseFile;
                        var fileName=dataList[i].fileName;

                        var blob=dataURLtoBlob(baseFile,dataType);
                        saveAs(blob, fileName );
                        dataList[i].baseFile="--";
                    }
                }
            }

           $("#docDownDiv").JSONView(data);
        },
        error: function(oData, oStatus, eErrorThrow) {
           console.log(oData);
            $("#docDownDiv").JSONView(oData.responseText);
        }
    };
    $.ajax(oAjaxOption);
}
function dataURLtoBlob(dataurl,type) {
   var
        bstr = atob(dataurl), n = bstr.length, u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: type });
}